package com.dmall.scf

import com.dmall.scf.action._
import com.dmall.scf.utils.{DateUtils, ExceptionUtil, SparkUtils}
import org.apache.spark.sql.SparkSession

import scala.collection.JavaConverters._
/**
 * @descrption
 * scf-spark
 * @author wangxuexing
 * @date 2019/12/26
 */
object HistoryDataInit {
  def main(args: Array[String]): Unit = {
    if(args == null || args.length < 3){
      throw new Exception("请指定Action及相应参数！格式：Action_Name startDate endDate, 如：HistoryFee90 2019-12-01 2020-01-31")
    }

    val dayList = DateUtils.getDays(args(1), args(2)).asScala
    val spark: SparkSession = SparkUtils.getEnv.spark
    dayList.foreach(day => {
      args(1) = day
      println(s"Type： ${args(0)}, Start run ${args(1)}, end ${args(2)}")
      try{
        args(0) match {
          case HistoryFangKuang180.CLASS_NAME => HistoryFangKuang180.execute(args, spark)
          case HistoryFee90.CLASS_NAME => HistoryFee90.execute(args, spark)
          case HistoryGoodIn20.CLASS_NAME => HistoryGoodIn20.execute(args, spark)
          case HistorySale90.CLASS_NAME => HistorySale90.execute(args, spark)
          case HistorySale180.CLASS_NAME => HistorySale180.execute(args, spark)
          case HistorySaleCost90.CLASS_NAME => HistorySaleCost90.execute(args, spark)
          case HistorySaleDays15.CLASS_NAME => HistorySaleDays15.execute(args, spark)
          case HistorySaleGross90.CLASS_NAME => HistorySaleGross90.execute(args, spark)
          case _ => throw new Exception(s"没有指定的Action：${args(0)}！")
        }
      } catch {
        case e: Exception => println(s"${args(0)} error： ${ExceptionUtil.getExceptionStack(e)}")
        case _ => println(s"${args(0)} error！ ")
      }
    })
    spark.stop()
  }
}
